//
//  TCLYCommentViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/9/3.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//
/*******************************************************************************************这个是评论的界面（在点击商品后 进入商品详细信息的时候，查看所有评论列表的界面）********************************************************************************************************************************************************/
#import "TCLYCommentViewController.h"
#import "TCLYCommentTableViewCell.h"
#import "TCLYHomeModel.h"
#import "UIImageView+WebCache.h"
#import "UIView+HB.h"

#import "ZLPhotoActionSheet.h"
#import "ZLShowBigImage.h"
#import "ZLDefine.h"
#import "ZLCollectionCell.h"
#import "Masonry.h"
#import "Macros.h"
@interface TCLYCommentViewController ()<UITableViewDataSource,  UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong)UITableView * commentTabelView;
@property (nonatomic, strong)TCLYHomeModel * homeModel;
@property (nonatomic, strong) NSArray *arrDataSources;
@end

@implementation TCLYCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    // Do any additional setup after loading the view.
    [self createView];
    [self createTitleView];
    NSLog(@"%ld",(unsigned long)self.IntegralCommentArr.count);
}
-(void)createTitleView{
    self.navigationController.navigationBar.barStyle = UIBarMetricsCompactPrompt;
    self.navigationController.navigationBar.backgroundColor = statusColor;
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenWidth, 20)];
    view.backgroundColor = navColor;
    [self.navigationController.navigationBar addSubview:view];
    
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"评论列表";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.frame = CGRectMake(0, 0, 50, 20);
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = titleLabel;
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(12, 22, 20, 20);
    [leftBtn setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    leftBtn.tag = 91;
    [leftBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
}
-(void)statusBtnAction:(id)sender{
    switch (((UIButton *)sender).tag) {
        case 91:{
            [self.navigationController popViewControllerAnimated:YES];
            self.navigationController.navigationBarHidden = YES;
            self.tabBarController.tabBar.hidden = YES;
        }
            break;
        case 92:{
            
        }
            break;
    }
}

-(void)createView{
    self.commentTabelView = [[UITableView alloc] init];
    self.commentTabelView.frame = CGRectMake(0, 0, screenWidth, screenHeight-64);
    self.commentTabelView.backgroundColor = bgColor;
    [self.commentTabelView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.commentTabelView.delegate = self;
    self.commentTabelView.dataSource = self;
    [self.view addSubview:self.commentTabelView];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.IntegralCommentArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"commentCell";
    TCLYCommentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[TCLYCommentTableViewCell alloc] initWithReuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSURL * url = [NSURL URLWithString:self.IntegralCommentArr[indexPath.row][@"upic"]];
    [cell.headerImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"LYJia2"]];
    cell.nickNameLabel.text = self.IntegralCommentArr[indexPath.row][@"uname"];
    
    if (self.isBaby) {
        cell.commentLabel.text = self.IntegralCommentArr[indexPath.row][@"content"];
        cell.timeLabel.text = self.IntegralCommentArr[indexPath.row][@"time"];
    }else{
        cell.commentLabel.text = self.IntegralCommentArr[indexPath.row][@"content"];
        cell.timeLabel.text = self.IntegralCommentArr[indexPath.row][@"time"];
    }

    
    CGSize titleSize = [cell.commentLabel sizeThatFits:CGSizeMake(cell.commentLabel.bounds.size.width, MAXFLOAT)];
    [cell.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(cell.mas_left).offset(6);
        make.top.mas_equalTo(cell.nickNameLabel.mas_bottom).offset(6);
        make.right.mas_equalTo(cell.mas_right).offset(-6);
        make.height.mas_equalTo(titleSize.height);
    }];

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(screenWidth/5, screenWidth/5);
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 1.5;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    cell.commentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50+titleSize.height, screenWidth-screenWidth/5-screenWidth/5/3-screenWidth/5/3, screenWidth/5) collectionViewLayout:layout];
    cell.commentCollectionView.backgroundColor = [UIColor whiteColor];
    cell.commentCollectionView.tag = 10000+indexPath.row;
    cell.commentCollectionView.dataSource = self;
    cell.commentCollectionView.delegate = self;
    [cell.commentCollectionView registerNib:[UINib nibWithNibName:@"ZLCollectionCell" bundle:kZLPhotoBrowserBundle] forCellWithReuseIdentifier:@"ZLCollectionCell"];
    [cell addSubview:cell.commentCollectionView];
    
    if (self.isBaby) {
        if ([self.IntegralCommentArr[indexPath.row][@"pic"] isEqual:@"1"]) {
            cell.commentCollectionView.hidden = YES;
            [cell.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(cell.commentLabel.mas_bottom);
                make.left.mas_equalTo(cell.mas_left).offset(6);
                make.right.mas_equalTo(cell.mas_right).offset(-6);
                make.height.mas_equalTo(20);
            }];
            cell.height = titleSize.height+70;
        }else{
            [cell.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(cell.commentCollectionView.mas_bottom);
                make.left.mas_equalTo(cell.mas_left).offset(6);
                make.right.mas_equalTo(cell.mas_right).offset(-6);
                make.height.mas_equalTo(20);
            }];
            cell.height = titleSize.height+150;
        }
    }else{
        if ([self.IntegralCommentArr[indexPath.row][@"pic"] isEqual:@"1"]) {
            cell.commentCollectionView.hidden = YES;
            [cell.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(cell.commentLabel.mas_bottom);
                make.left.mas_equalTo(cell.mas_left).offset(6);
                make.right.mas_equalTo(cell.mas_right).offset(-6);
                make.height.mas_equalTo(20);
            }];
            cell.height = titleSize.height+70;
        }else{
            [cell.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(cell.commentCollectionView.mas_bottom);
                make.left.mas_equalTo(cell.mas_left).offset(6);
                make.right.mas_equalTo(cell.mas_right).offset(-6);
                make.height.mas_equalTo(20);
            }];
            cell.height = titleSize.height+150;
        }
    }
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.isBaby) {
        if ([self.IntegralCommentArr[collectionView.tag-10000][@"pic"] isEqual:@"1"]) {
            self.intefralImageArr = nil;
            return 0;
        }else{
            self.intefralImageArr = self.IntegralCommentArr[collectionView.tag-10000][@"pic"];
            NSLog(@"tupian%@",self.IntegralCommentArr[collectionView.tag-10000][@"pic"]);
            return self.intefralImageArr.count;
        }
    }else{
        if ([self.IntegralCommentArr[collectionView.tag-10000][@"comment_pic"] isEqual:@"1"]) {
            self.intefralImageArr = nil;
            return 0;
        }else{
            self.intefralImageArr = self.IntegralCommentArr[collectionView.tag-10000][@"pic"];
            NSLog(@"tupian%@",self.IntegralCommentArr[collectionView.tag-10000][@"pic"]);
            return self.intefralImageArr.count;
        }
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZLCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZLCollectionCell" forIndexPath:indexPath];
    cell.btnSelect.hidden = YES;
    NSURL * url = [NSURL URLWithString:self.intefralImageArr[indexPath.row]];
    [cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"LYJia2"]];
    cell.imageView.contentMode = UIViewContentModeScaleAspectFill;    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZLCollectionCell *cell = (ZLCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [ZLShowBigImage showBigImage:cell.imageView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//改变行的高度（实现主个代理方法后 rowHeight 设定的高度无效）
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    return cell.frame.size.height;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
